package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.bo.MarketGoodsBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.service.admin.MarketGoodsService;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

// 1.接受请求参数 抓包  2.处理业务逻辑  3.返回响应 抓公网的包，和公网上返回的响应一样
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController extends HttpServlet {
    @Autowired
    private MarketGoodsService goodsService;

    @Autowired
    ObjectMapper objectMapper;

    @GetMapping("list")
    public Object list(HttpServletRequest req) {
        //获取请求参数
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String goodsSn = req.getParameter("goodsSn");
        String name = req.getParameter("name");
        String goodsId = req.getParameter("goodsId");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        Integer page = null;
        Integer limit = null;
        //如果输入参数不是数字
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (Exception e) {
            //输出参数错误
            return ResponseUtil.badArgument();
        }
        //处理业务逻辑
        List<MarketGoods> marketGoods = goodsService.list(page, limit, goodsSn, name, sort, order, goodsId);
        //返回响应
        return ResponseUtil.okList(marketGoods);
    }

    @GetMapping("detail")
    public Object detail(HttpServletRequest req) {
        String id = req.getParameter("id");
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        Integer goodsId = Integer.parseInt(id);
        Map<String, Object> data = goodsService.detail(goodsId);
        return ResponseUtil.ok(data);
    }

    @GetMapping("catAndBrand")
    public Object catAndBrand() {
        Map<String, Object> data = goodsService.catAndBrand();
        return ResponseUtil.ok(data);
    }


    @PostMapping("delete")
    public Object delete(HttpServletRequest req) throws IOException {
        BufferedReader requestBody = req.getReader();
        MarketGoods goods = objectMapper.readValue(requestBody, MarketGoods.class);
        goodsService.delete(goods);
        return ResponseUtil.ok();
    }

    @PostMapping("create")
    public Object create(HttpServletRequest req) throws IOException {
        String requestBody = req.getReader().readLine();
        MarketGoodsBo marketGoodsBo = objectMapper.readValue(requestBody, MarketGoodsBo.class);
        goodsService.create(marketGoodsBo.getGoods(), marketGoodsBo.getProducts(), marketGoodsBo.getSpecifications(), marketGoodsBo.getAttributes());
        return ResponseUtil.ok();
    }

    @PostMapping("update")
    public Object update(HttpServletRequest req) throws IOException {
        String requestBody = req.getReader().readLine();
        MarketGoodsBo marketGoodsBo = objectMapper.readValue(requestBody, MarketGoodsBo.class);
        String name = marketGoodsBo.getGoods().getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        goodsService.update(marketGoodsBo.getGoods(), marketGoodsBo.getProducts(), marketGoodsBo.getSpecifications(), marketGoodsBo.getAttributes());
        return ResponseUtil.ok();
    }

}

